<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 教师管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
          >批量删除</el-button
        >

        <el-input
          v-model="query.school"
          placeholder="学校名"
          class="handle-input mr10"
        ></el-input>

        <el-input
          v-model="query.account"
          placeholder="账号"
          class="handle-input mr10"
        ></el-input>
        <el-input
          v-model="query.name"
          placeholder="姓名"
          class="handle-input mr10"
        ></el-input>
        <el-input
          v-model="query.subject"
          placeholder="科目"
          class="handle-input mr10"
        ></el-input>
        <el-input
          v-model="query.duty"
          placeholder="职务"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
        <el-button type="primary" icon="el-icon-refresh-right" @click="reset"
          >重置</el-button
        >
        <el-button
          type="primary"
          style="float: right"
          icon="el-icon-plus"
          @click="addEdit()"
          >添加</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-descriptions>
              <el-descriptions-item label="用户名">{{
                scope.row.name
              }}</el-descriptions-item>
              <el-descriptions-item label="身份证号">{{
                scope.row.idCard
              }}</el-descriptions-item>
              <el-descriptions-item label="性别">
                <el-tag :type="scope.row.sex === true ? 'success' : 'danger'">
                  {{ scope.row.sex === true ? "男" : "女" }}
                </el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="办公室">
                <el-tag size="small">{{ scope.row.office }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="授课班级">
                <el-tag
                  type="warning"
                  size="small"
                  v-for="(c, index) in scope.row.classroomList"
                  :key="index"
                >
                  {{ c }}
                </el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="职务">
                <el-tag size="small">{{ scope.row.duty }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="联系电话">
                {{ scope.row.phone }}
              </el-descriptions-item>
              <el-descriptions-item label="邮箱">
                {{ scope.row.email }}
              </el-descriptions-item>
            </el-descriptions>
          </template>
        </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column label="账号" prop="account"> </el-table-column>
        <el-table-column label="头像(查看大图)" align="center">
          <template slot-scope="scope">
            <el-image
              class="table-td-thumb"
              :src="scope.row.avatar"
              :preview-src-list="[scope.row.avatar]"
            ></el-image>
          </template> </el-table-column
        ><el-table-column prop="school" label="学校"></el-table-column>
        <el-table-column prop="college" label="学院"></el-table-column>
        <el-table-column prop="subject" label="学科"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form ref="form" :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="ID">
              <el-input v-model="form.id" disabled></el-input>
            </el-form-item>
            <el-form-item label="用户名">
              <el-input v-model="form.name"></el-input>
            </el-form-item>

            <el-form-item label="账号">
              <el-input v-model="form.account"></el-input>
            </el-form-item>
            <el-form-item label="头像上传">
              <el-upload
                class="avatar-uploader"
                action="http://127.0.0.1:8080/system/headImgUpload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                style="width: 180px"
              >
                <img v-if="form.avatar" :src="form.avatar" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="学校">
              <el-select
                v-model="form.school"
                placeholder="点击选择学校"
                @change="getCollegeOption"
                class="optionStyle"
              >
                <el-option
                  :label="s.name"
                  :value="s.id"
                  v-for="(s, index) in schoolOption"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学院">
              <el-select
                v-model="form.college"
                placeholder="点击选择学院"
                @change="getMajorOption"
                class="optionStyle"
              >
                <el-option
                  :label="s.name"
                  :value="s.id"
                  v-for="(s, index) in collegeOption"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学科">
              <el-input v-model="form.subject"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份账号">
              <el-input v-model="form.idCard"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="form.sex">
                <el-radio :label="true">男</el-radio>
                <el-radio :label="false">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="办公室">
              <el-input v-model="form.office"></el-input>
            </el-form-item>
            <!-- <el-form-item label="所在专业">
              <el-select
                v-model="form.major"
                placeholder="点击选择专业"
                @change="getClassOption"
                class="optionStyle"
              >
                <el-option
                  :label="s.name"
                  :value="s.id"
                  v-for="(s, index) in majorOption"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="授课班级">
              <el-select v-model="form.classroomList" multiple placeholder="">
                <el-option
                  :label="c.no"
                  :value="c.id"
                  v-for="(c, index) in classOption"
                  :key="index"
                  class="optionStyle"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="职务">
              <el-input v-model="form.duty"></el-input>
            </el-form-item>
            <el-form-item label="联系电话">
              <el-input v-model="form.phone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="form.email"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增 -->
    <el-dialog title="编辑" :visible.sync="editVisible2" width="50%">
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="name">
              <el-input v-model="form.name"></el-input>
            </el-form-item>

            <el-form-item label="账号" prop="account">
              <el-input v-model="form.account"></el-input>
            </el-form-item>
            <el-form-item label="头像上传">
              <el-upload
                class="avatar-uploader"
                action="http://127.0.0.1:8080/system/headImgUpload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                style="width: 180px"
              >
                <img v-if="form.avatar" :src="form.avatar" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="学校" prop="school">
              <el-select
                v-model="form.school"
                placeholder="点击选择学校"
                @change="getCollegeOption"
                class="optionStyle"
              >
                <el-option
                  :label="s.name"
                  :value="s.id"
                  v-for="(s, index) in schoolOption"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学院" prop="college">
              <el-select
                v-model="form.college"
                placeholder="点击选择学院"
                @change="getMajorOption"
                class="optionStyle"
              >
                <el-option
                  :label="s.name"
                  :value="s.id"
                  v-for="(s, index) in collegeOption"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学科">
              <el-input v-model="form.subject"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份账号">
              <el-input v-model="form.idCard"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="form.sex">
                <el-radio :label="true">男</el-radio>
                <el-radio :label="false">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="办公室">
              <el-input v-model="form.office"></el-input>
            </el-form-item>
            <!-- <el-form-item label="所在专业" prop="major">
              <el-select
                v-model="form.major"
                placeholder="点击选择专业"
                @change="getClassOption"
                class="optionStyle"
              >
                <el-option
                  :label="s.name"
                  :value="s.id"
                  v-for="(s, index) in majorOption"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="授课班级" prop="classroomList">
              <el-select
                v-model="form.major"
                placeholder="点击选择专业"
                @change="getClassOption"
                class="optionStyle"
              >
                <el-option
                  :label="s.name"
                  :value="s.id"
                  v-for="(s, index) in majorOption"
                  :key="index"
                ></el-option>
              </el-select>
              <el-select v-model="form.classroomList" multiple placeholder="">
                <el-option
                  :label="c.no"
                  :value="c.id"
                  v-for="(c, index) in classOption"
                  :key="index"
                  class="optionStyle"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="职务" prop="duty">
              <el-input v-model="form.duty"></el-input>
            </el-form-item>
            <el-form-item label="联系电话">
              <el-input v-model="form.phone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="form.email"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="resetDialog" plain>重 置</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { search, add, del, update, delList } from "../../../api/teacher.js";

import {
  searchSchool,
  searchCollege,
  searchMajor,
  searchClass,
} from "../../../api/subjects";
export default {
  name: "basetable",
  data() {
    return {
      query: {
        name: null,
        account: null,
        duty: null,
        school: null,
        subject: null,
        pageIndex: 1,
        pageSize: 10,
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      editVisible2: false,
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
      schoolOption: [],
      collegeOption: [],
      majorOption: [],
      classOption: [],
      rules: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        account: [
          { required: true, message: "请输入", trigger: "blur" },
          {
            require: true,
            min: 10,
            max: 10,
            message: "长度在10个字符",
            trigger: "blur",
          },
        ],
        school: [{ required: true, message: "请输入", trigger: "change" }],
        college: [{ required: true, message: "请输入", trigger: "change" }],
        classroomList: [{ required: true, message: "请输入", trigger: "blur" }],
        duty: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getData();
    searchSchool()
      .then((result) => {
        if (result.status == "200") {
          this.schoolOption = result.data;
        }
      })
      .catch((err) => {});
  },
  methods: {
    // 重置弹出框
    resetDialog() {
      this.form = {};
    },

    handleAvatarSuccess(res, file) {
      this.form.avatar = "http://" + res.data;
      // this.form.avatar = URL.createObjectURL(file.raw);
    },
    getCollegeOption(val) {
      searchCollege(val)
        .then((result) => {
          if (result.status == "200") {
            this.collegeOption = result.data;
          }
        })
        .catch((err) => {});
    },
    getMajorOption(val) {
      searchMajor(val)
        .then((result) => {
          if (result.status == "200") {
            this.majorOption = result.data;
          }
        })
        .catch((err) => {});
    },

    getClassOption(val) {
      console.log(val);
      searchClass(val)
        .then((result) => {
          if (result.status == "200") {
            console.log(result.data);
            this.classOption = result.data;
          }
        })
        .catch((err) => {});
    },

    // 保存添加
    saveAdd() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.editVisible2 = false;
          console.log(this.form);
          add(this.form)
            .then((result) => {
              if (result.status == "200") {
                this.$message.success(`新增数据成功！`);
                this.getData();
              }
            })
            .catch((err) => {});
        } else {
          return false;
        }
      });
    },

    // 重置
    reset() {
      this.query.name = null;
      this.query.account = null;
      this.query.school = null;
      this.query.duty = null;
      this.query.subject = null;
      this.getData();
    },

    // 重置弹出框
    resetDialog() {
      this.form = {};
    },
    // 获取 easy-mock 的模拟数据
    getData() {
      search(this.query)
        .then((result) => {
          if ((result.status = "200")) {
            this.tableData = result.data.list;
            this.pageTotal = result.data.totalCount;
          }
        })
        .catch((err) => {});
      this.tableData = this.list;
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          del(row.id)
            .then((result) => {
              if (result.status == "200") {
                this.$message.success("删除成功");
                this.tableData.splice(index, 1);
              } else {
                this.$message.danger(result.status + "：操作失败");
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = "";
      var ids = new Array();
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + " ";
        ids.push(this.multipleSelection[i].id);
      }
      delList(ids)
        .then((result) => {
          if (result.status == "200") {
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
            this.getData();
          } else {
            this.$message.danger("操作失败！");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      console.log(ids);
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 添加操作
    addEdit() {
      this.editVisible2 = true;
    },
    // 保存编辑
    saveEdit() {
      this.editVisible = false;
      this.$set(this.tableData, this.idx, this.form);
      console.log(this.form);
      update(this.form)
        .then((result) => {
          if (result.status == "200") {
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.getData();
          }
        })
        .catch((err) => {});
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 150px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.avatar-uploader .el-upload {
  width: 100%;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader .el-upload--text {
  width: 100% !important;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100%;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar-uploader .avatar {
  width: 100%;
  height: 178px;
  display: block;
}
.optionStyle {
  width: 100%;
}
</style>
